package com.ces.knacky.restws.service;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ces.knacky.model.Auction;
import com.ces.knacky.model.Bid;
import com.ces.knacky.services.AuctionService;

@Path("auctionService")
@Component
public class AuctionRService {
	
	@Autowired
	AuctionService auctionService;
	
	@GET
	@Produces({"application/xml","application/json"})
	public List<Auction> getAllAuctions()
	{
		return (List<Auction>) auctionService.list("SELECT auction from Auction auction");
	}
	
	@GET
	@Produces({"application/xml", "application/json"})
	@Path("{id}")
	public List<Auction> getAuction(@PathParam("id") int id)
	{
		return (List<Auction>) auctionService.list("SELECT auction from Auction auction where auction.id = " + id);
	}
	
	@POST
	@Consumes({"application/json", "application/xml"})
	@Produces("text/plain")
	@Path("/add")
	public String addAuction(Auction auction)
	{
		
		if(auctionService.add(auction) == false)
		{
			System.out.println("Error in adding auction");
		}
		
		return "Auction added";
	
	}
	
	@PUT
	@Consumes({"application/json", "application/xml"})
	@Produces("text/plain")
	@Path("/update")
	public String updateAuction(Auction auction)
	{
		System.out.println("Update: " + auction);
		if(auctionService.update(auction) == false)
		{
			System.out.println("Error in updating auction");
		}
		return "Auction updated";
	
	}
	
	@DELETE
	@Produces("text/plain")
	@Path("{id}")
	public String deleteAuction(@PathParam("id") int id)
	{
		Auction auction = null;
		if( (auction = (Auction) auctionService.load(Auction.class, id)) == null)
		{
			return "No Record found";			
		}
		
		System.out.println("Deleted Id: " + id);
		
		if(auctionService.delete(auction) == false)
		{
			System.out.println("Error in deleting auction");
		}
		return "Auction deleted";
	
	}
	
	@POST
	@Produces(MediaType.TEXT_PLAIN)
	@Path("{auctionId}/item/{itemId}/bidPrice/{bidPrice}")
	public String placeBid(@PathParam("itemId") int itemId,@PathParam("auctionId")int auctionId,@PathParam("bidPrice")Float bidPrice)
	{
		if(auctionService.placeBid(itemId, auctionId, bidPrice) == false)
		{
			return "Error in placing Bid";
		}
		
		return "Bid placed";
	}
	
	@POST
	@Produces(MediaType.TEXT_PLAIN)
	@Path("bid/approve/{ids}")
	public String approveBid(@PathParam("ids") String selectedIds)
	{
		if(auctionService.approveBid(selectedIds) == false)
		{
			return "Error in approving Bid";
		}
		
		return "Bid approved";
		
	}
	
	@GET
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	@Path("bid/place")
	public List<Bid> loadPlacedBid() 
	{
		List<Bid> lbwa = auctionService.loadPlacedBid();
		
		return lbwa;
	}

}
